﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Xml;
using LTP.Common;
namespace Oauth4Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
                if (Request["oauth_verifier"] != null)
                {
                    httpRequest.Token = Session["oauth_token"].ToString();
                    httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
                    httpRequest.Verifier = Request["oauth_verifier"];
                    httpRequest.GetAccessToken();
                    Session["oauth_token"] = httpRequest.Token;
                    Session["oauth_token_secret"] = httpRequest.TokenSecret;
                    Response.Redirect("/membersHome/Sample.aspx");
                    
                }
                if (Session["oauth_token"]!=null)
                    this.sinaButton.Text = "连接成功";
            }         
        }

        protected void sinaButton_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
            httpRequest.GetRequestToken();
            string url = httpRequest.GetAuthorizationUrl();
            Session["oauth_token"] = httpRequest.Token;
            Session["oauth_token_secret"] = httpRequest.TokenSecret;
            Response.Redirect(url + "&oauth_callback=http://localhost:4040/membersHome/Sample.aspx");
        }

        protected void invoke_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string basic = "http://api.t.sina.com.cn/";
            string url = "";
            switch (this.DropDownList.SelectedIndex)
            {
                case 0:
                    url = basic + "statuses/public_timeline.xml";
                    break;
                case 1:
                    url = basic + "statuses/friends_timeline.xml";
                    break;
                case 2:
                    url = basic + "statuses/user_timeline.json";
                    break;
                case 3:
                    url = basic + "statuses/mentions.xml";
                    break;
                case 4:
                    url = basic + "statuses/comments_timeline.json";
                    break;
                case 5:
                    url = basic + "statuses/comments_by_me.xml";
                    break;
                case 6:
                    url = basic + "statuses/followers.xml";
                    break;
                default:
                    break;
            }

            DataSet xds = new DataSet();
            //FileStream fsReadXml = new FileStream(url, FileMode.OpenOrCreate, FileAccess.Read);

            //XmlTextReader myXmlReader = new XmlTextReader(fsReadXml);

            //xds.ReadXml(myXmlReader);

            //myXmlReader.Close();

            #region 下载表情到本地数据库代码 勿动
            //url = "http://api.t.sina.com.cn/emotions.xml";
            
            //this.resultText.Text = httpRequest.Request(url, String.Empty);
            //xds = GetDataSetByXml(this.resultText.Text);
            //TCS.Model.Weibo.emotion model = new TCS.Model.Weibo.emotion();
            //TCS.BLL.Weibo.emotion bll = new TCS.BLL.Weibo.emotion();
            //for (int i = 0; i < xds.Tables[0].Rows.Count; i++)
            //{
                
            //    model.phrase = xds.Tables[0].Rows[i]["phrase"].ToString();
            //    model.type = xds.Tables[0].Rows[i]["type"].ToString();
            //    model.url = xds.Tables[0].Rows[i]["url"].ToString();
            //    model.is_hot = xds.Tables[0].Rows[i]["is_hot"].ToString();
            //    model.is_common = xds.Tables[0].Rows[i]["is_common"].ToString();
            //    model.order_number = xds.Tables[0].Rows[i]["order_number"].ToString();
            //    model.category = xds.Tables[0].Rows[i]["category"].ToString();
            //    bll.Add(model);
            //}

            #endregion
            ////JSONObject json = JSONConvert.DeserializeObject("{\"domain\":\"mzwu.com\",\"two\":{\"oneKey\":\"one\",\"twoArray\":[1,2,3,4]},\"years\":[2006,2007,2008,2009,2010]}");
          
            //DL.DataSource = listObj;
            //DL.DataBind();
            this.resultText.Text = httpRequest.Request(url,string.Empty);
            List<LTP.Common.Comment> listObj = JSONConvert.DeserializeJsonToListObjectComment(this.resultText.Text);
            string a = "b";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            var url = "http://api.t.sina.com.cn/statuses/update.xml?";
            resultTextBox2.Text = httpRequest.Request(url, "status=" + HttpUtility.UrlEncode(statusText.Text));
        }

        protected void upload_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.POST) as HttpPost;
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = "http://api.t.sina.com.cn/statuses/upload.xml?";
            this.resultTextBox2.Text = httpRequest.RequestWithPicture(url, "status=" + HttpUtility.UrlEncode(statusText.Text), filepath.Text);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            var url = "http://api.t.sina.com.cn/direct_messages/new.xml?id=2282241151&text=tttt";
           // var url = "http://api.t.sina.com.cn/search.json?q=" + txtMsgText.Text;
            TextBox2.Text = httpRequest.Request(url, "");
        }
        protected static DataSet GetDataSetByXml(string xmlData)
        {
            try
            {
                DataSet ds = new DataSet();

                using (StringReader xmlSR = new StringReader(xmlData))
                {

                    //ds.ReadXml(xmlSR, XmlReadMode.InferTypedSchema); //忽视任何内联架构，从数据推断出强类型架构并加载数据。如果无法推断，则解释成字符串数据
                    ds.ReadXml(xmlSR);
                    if (ds.Tables.Count > 0)
                    {
                        return ds;
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        } 


    }
}
